﻿<#-- 
 
 Control reorderListControl c# para la tabla  ${table}...
 Author : Luis Molina...
 
 -->
<#-- variables used internally by the generator... -->
<#assign nameFile="${table}">
<#assign extensionFile="cs">
<#assign languageGenerated="c#">
<#assign description="c#">
<#assign targetDirectory="">
<#assign appliesToAllTables="true">
<#-- END variables used internally by the generator... -->
 
<cc1:ReorderList ID="rl${table}" runat="server"
            AllowReorder="False"
            PostBackOnReorder="True"
            SortOrderField="${table.getKey()}"
            ItemInsertLocation="Beginning" 
            onitemreorder="rl${table}_ItemReorder" 
            onitemcommand="rl${table}_ItemCommand"
			OnItemDataBound="rl${table}_ItemDataBound">
            
                <ItemTemplate>
                    <asp:HiddenField  ID="${table.getKey()}" runat="server" Value='<% #Eval("${table.getKey()}") %>' /> 
                    <asp:Label ID="lab${table.getFieldDescription()}" runat="server" Text='<% #Eval("${table.getFieldDescription()}") %>' />
                    <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit" Text="Edit" />
                    <asp:LinkButton ID="LinkButton3" runat="server" CommandName="Delete" Text="Delete" />
                </ItemTemplate>
                
                <ReorderTemplate>
                    <div style="width: 300px; height: 20px; border: dotted 2px black;">
                        &nbsp;
                    </div>
                </ReorderTemplate>
                
                <InsertItemTemplate>
				<#list table.getFields() as field>
					 <#-- segun el tipo de campo...   -->
					 <#switch field.getType()>
						  <#case "_int">
							<asp:Label ID="lab${field}" runat="server" Text="${field}"></asp:Label>
							<asp:TextBox ID="txt${field}" runat="server" Text='<%# Bind("${field}") %>'></asp:TextBox><br />
							 <#break>
						  <#case "_string">
							<asp:Label ID="lab${field}" runat="server" Text="${field}"></asp:Label>
							<asp:TextBox ID="txt${field}" runat="server" Text='<%# Bind("${field}") %>'></asp:TextBox><br />
							 <#break>
						  <#case "_date">
							<asp:Label ID="lab${field}" runat="server" Text="${field}"></asp:Label>
							<asp:TextBox ID="txt${field}" runat="server" Text='<%# Bind("${field}") %>'></asp:TextBox><br />
							 <#break>
						  <<#case "_boolean">
							<asp:Label ID="lab${field}" runat="server" Text="${field}"></asp:Label>
							<asp:TextBox ID="txt${field}" runat="server" Text='<%# Bind("${field}") %>'></asp:TextBox><br />
							 <#break>
						  <#default>
							<asp:Label ID="lab${field}" runat="server" Text="${field}"></asp:Label>
							<asp:TextBox ID="txt${field}" runat="server" Text='<%# Bind("${field}") %>'></asp:TextBox><br />
					</#switch>  				 
				</#list>                    
                    <asp:Button ID="btnInsert" runat="server" Text="Add Link" CommandName="Insert" />
                </InsertItemTemplate>
                
                <EditItemTemplate>
                    // poner a HiddenField los que no se quiera mostrar						
					<#list table.getFields() as field>
						<#-- segun el tipo de campo...   -->
						 <#switch field.getType()>
							  <#case "_int">
								<asp:TextBox ID="txt${field}" runat="server" Text='<%# Bind("${field}") %>'/>
								 <#break>
							  <#case "_string">
								<asp:TextBox ID="txt${field}" runat="server" Text='<%# Bind("${field}") %>'/>
								 <#break>
							  <#case "_date">
								<asp:TextBox ID="txt${field}" runat="server" Text='<%# Bind("${field}") %>'/>
								 <#break>
							  <<#case "_boolean">
								<asp:TextBox ID="txt${field}" runat="server" Text='<%# Bind("${field}") %>'/>
								 <#break>
							  <#default>
								<asp:TextBox ID="txt${field}" runat="server" Text='<%# Bind("${field}") %>'/>
						</#switch>  				 
					</#list> 
					<#list table.getFields() as field>
						<#-- segun el tipo de campo...   -->
						 <!-- <asp:HiddenField ID="txt${field}" runat="server" Value='<%# Bind("${field}") %>'/> -->
					</#list> 
                    
                    <asp:LinkButton ID="LinkButton2" runat="server" CommandName="Update" Text="Update" />
                    <asp:LinkButton ID="LinkButton4" runat="server" CommandName="Cancel" Text="Cancel" />                      
                </EditItemTemplate>
                
            </cc1:ReorderList>
            
			
			 <asp:ObjectDataSource ID="ods${table}" runat="server" DataObjectTypeName="${table}"
				DeleteMethod="Delete" InsertMethod="InsertBll"
				SelectMethod="Get${table}" TypeName="${table}Bll" UpdateMethod="UpdateBll">            
			</asp:ObjectDataSource>
 
 
 
	// llamar a esta funcion en !page.ispostback
   private void bindData()
        {
            rl${table}.EditItemIndex = -1;
            rl${table}.DataKeyField = "${table.getKey()}";
            rl${table}.DataSourceID = "ods${table}";
            rl${table}.DataBind();
        }
 
	// codigo para el reorderListControl
	protected void rl${table}_ItemReorder(object sender, ReorderListItemReorderEventArgs e)
	{
		
	}

	    protected void rl${table}_ItemDataBound(object sender, ReorderListItemEventArgs e)
        {

            if (e.Item.DataItem != null)
            {
			
				// get the item
                ${table} ${table}x = (${table})e.Item.DataItem;
				
                //// si tenemos un combo... vamos a seleccionar en el combo el valor correcto
                //DropDownList lista = null;
                //lista = (DropDownList)e.Item.FindControl("cmbTiposAccesibilidades");
                //lista.SelectedItem = e.Item;
                ////lblFileName.Text = ((ObjetoDeLaLista)e.Item.DataItem).Nombre;
            }
        }
	
  
	protected void rl${table}_ItemCommand(object sender, ReorderListCommandEventArgs e)
	{
		switch (e.CommandName)
		{
			case "Cancel":
				break;
			case "Insert":
				break;
			case "Delete":
				break;
			case "Update":
				break;
			case "Edit":
				// string id = "";
                // id = ((HiddenField)e.Item.FindControl("${table.getKey()}")).Value;
				break;
			default:
				break;
		}
	}

 
 
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.IO;
using System.Data;
using System.Web;
using System.ComponentModel;


[System.ComponentModel.DataObject]
public partial class ${table}Bll
{
 
  [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)] 
  public static List< ${table} > Get${table}()
    {
         List< ${table} > lista${table}= new  List< ${table} >();
        dbClass db=new dbClass(ctes.conStringAdoGeneral);
        
        try
        {
            StringBuilder sb= new StringBuilder();
           
            sb.Append(" select ");
            sb.Append("${table.getListWithAllFields()}");
            sb.Append(" from ${table}");
            System.Data.DataSet ds = db.sqlDataset(sb.ToString());

             foreach (DataRow row in ds.Tables[0].Rows)
            {
                ${table} new${table}= new ${table}();
                <#assign count=0>		
				<#list table.getFields() as field> 
				 <#switch field.getType()>
						  <#case "_int">
							 new${table}.${field} = sf.entero(row["${field}"]);
							 <#break>
						  <#case "_string">
							 new${table}.${field} = sf.cadena(row["${field}"]);
							 <#break>
						  <#case "_date">
							 new${table}.${field} = sf.fecha(row["${field}"].ToString());
							 <#break>
						  <<#case "_boolean">
							 new${table}.${field} = sf.Bool(row["${field}"]);
							 <#break>
						  <#default>
							 new${table}.${field} = sf.cadena(row["${field}"]);
				</#switch>  
			 <#assign count = count+1>
			</#list>	

                lista${table}.Add(new${table} );
                new${table} =null;
            }
        }
        catch (System.Exception ex)
        {}
        finally
        {
         db.Dispose();
        } 

        //authors.Sort(New AuthorComparer(sortExpression))
        return lista${table};
   }

 
[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
 public static string Insert(${table} obj${table})
    {            
        System.Text.StringBuilder sqlt = new System.Text.StringBuilder();
        string retorno = "";
        dbClass db = new dbClass(ctes.conStringAdoGeneral);
        SqlDataReader reg;
        try
        {
        sqlt.Append( " insert into ${table} (${table.getListWithAllFields()})"); 
        sqlt.Append( " ) values (");
			<#assign count=0>	
			<#list table.getFields() as field> 
				<#if (!field.getisKey())>
					 <#-- segun el tipo de campo...   -->
					 <#switch field.getType()>
							  <#case "_int">
			 					 sqlt.Append( sf.mySql(obj${table}.${field}) + " <#if (table.getNumberOfFields() -  count  != 1)>,</#if>") ;
								 <#break>
							  <#case "_string">
								 sqlt.Append( sf.mySql(obj${table}.${field}) + " <#if (table.getNumberOfFields() -  count  != 1)>,</#if>") ;
								 <#break>
							  <#case "_date">
								 sqlt.Append( sf.mySql(obj${table}.${field}) + " <#if (table.getNumberOfFields() -  count  != 1)>,</#if>")  ;
								 <#break>
							  <<#case "_boolean">
								 sqlt.Append( sf.mySql(obj${table}.${field})  + " <#if (table.getNumberOfFields() -  count  != 1)>,</#if>");
								 <#break>
							  <#default>
								 sqlt.Append( sf.mySql(obj${table}.${field}) + " <#if (table.getNumberOfFields() -  count  != 1)>,</#if>") ;
					</#switch>  
				</#if>
			 <#assign count = count+1>
			</#list>	 
			sqlt.Append(" )");

            db.ejecutar(sqlt.ToString());
            reg = db.sql("select @@identity as id from ${table}");
            if (reg.Read())
                {
                  retorno = sf.cadena(reg["id"].ToString());
                }
                reg.Close();
            }
                 
            catch (Exception ex)
            {
                // Argument is optional, no "When" keyword 
            }
            finally
            {
                db.Dispose();
              
            }
            return retorno;
       }

	
	public static void Delete(${table} obj${table})
    {
        string sqlt;
        dbClass db = new dbClass(ctes.conStringAdoGeneral);
        try { 
        sqlt = " delete from ${table} ";
        sqlt += " where 1!=0";  
        
        <#assign count=0>	
        <#list table.getFields() as field>
            <#if (field.getisKey())>
                <#if (field.getType()=="string")>
                    sqlt += " and ${field}='" + sf.cadena(obj${table}.${field})';
                <#else>
                     sqlt += " and ${field}=" + sf.cadena(obj${table}.${field});
                </#if>
            </#if>
         <#assign count = count+1>
		</#list>
           db.ejecutar(sqlt);

           }
           catch (Exception ex)
           {
               // Argument is optional, no "When" keyword 
           }
           finally
           {
               db.Dispose();

           }
    }
	
	
	[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
	public static bool UpdateBll(${table} obj${table})
		{
            SqlDataReader reg;
            System.Text.StringBuilder sqlt = new System.Text.StringBuilder();
            dbClass db = new dbClass(ctes.conStringAdoGeneral);
            try
            {
            sqlt.Append(" update ${table} set");
		<#assign count=0>		
		<#list table.getFields() as field>
		 	 <#-- segun el tipo de campo...   -->
			 <#switch field.getType()>
				  <#case "_int">
						sqlt.Append(" ${field}=" + sf.cadena(obj${table}.${field})+ "<#if (table.getNumberOfFields() -  count  != 1)>,</#if> ") ;
					 <#break>
				  <#case "_string">
					sqlt.Append(" ${field}='" + sf.cadena(obj${table}.${field})+ "'<#if (table.getNumberOfFields() -  count  != 1)>,</#if> "); 
					 <#break>
				  <#case "_date">
					  sqlt.Append(" ${field}='" + sf.fechaIso(obj${table}.${field}) + "'<#if (table.getNumberOfFields() -  count  != 1)>,</#if> " );
					 <#break>
				  <#case "_boolean">
					 sqlt.Append(" ${field}=" + sf.Bool(obj${table}.${field}) + "<#if (table.getNumberOfFields() -  count  != 1)>,</#if> ");
					 <#break>
				  <#default>
					sqlt.Append(" ${field}='" + sf.cadena(obj${table}.${field})+ "'<#if (table.getNumberOfFields() -  count  != 1)>,</#if> ") ;
			</#switch> 			 
		 <#assign count = count+1>
		</#list>
		          
		sqlt.Append(" where  ${table.getKey()}=" + sf.cadena(obj${table}.${table.getKey()}));        
		reg = db.sql(sqlt.ToString());
             return (reg!=null);
              }   
            catch (Exception ex)
            {
                // Argument is optional, no "When" keyword 
            }
            finally
            {
                db.Dispose();              
            }
             return true;
       }

	
 
}
